package com.huanglj.taobaounion.base;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.view.customView.LoadingView;
import com.huanglj.taobaounion.view.customView.LoopViewPager;

import java.util.zip.Inflater;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private static final String TAG = "BaseFragment";

    private Unbinder mBind;
    private FrameLayout mBaseContainer;
    private State currentState = State.NONE;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;
    private LinearLayout mErrorText;



    public BaseFragment() {
        // Required empty public constructor
    }

    /**
     * 定义各种状态
     */
    public enum State{
        NONE,LOADING,SUCCESS,EMPTY,ERROR
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = loadRootView(inflater,container);
        mBaseContainer = rootView.findViewById(R.id.base_container);
        // Inflate the layout for this fragment
        loadStateView(inflater,container);
        mBind = ButterKnife.bind(this, rootView);
        initView(rootView);
        initPresenter();
        loadData();
        initListener();
        LogUtil.d(TAG,"Fragment onCreateView");
        return rootView;
    }

    /**
     * 子类设置监听事件
     */
    protected void initListener(){};


    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    private void loadStateView(LayoutInflater inflater, ViewGroup container) {
        mLoadingView = loadLoadingView(inflater,container);
        mBaseContainer.addView(mLoadingView);

        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);

        mEmptyView = loadEmptyView(inflater,container);
        mBaseContainer.addView(mEmptyView);

        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);

        setUpState(State.NONE);
    }

    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container){
        return inflater.inflate(R.layout.base_empty_layout,container,false);
    };

    private View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        View errorView = inflater.inflate(R.layout.base_error_layout, container, false);
        mErrorText = errorView.findViewById(R.id.base_error_text);
        mErrorText.setOnClickListener(v -> {
            onRetryClick();

        });
        return errorView;
    }
    //点击重新加载数据
    protected void onRetryClick() {
        loadData();
    }

    public void setUpState(State state){
        currentState = state;
        mLoadingView.setVisibility(currentState== State.LOADING ? View.VISIBLE : View.GONE);
        mSuccessView.setVisibility(currentState== State.SUCCESS ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(currentState== State.ERROR ? View.VISIBLE : View.GONE);
        mEmptyView.setVisibility(currentState==State.EMPTY ? View.VISIBLE : View.GONE);
        if (mLoadingView.getVisibility() != View.VISIBLE) {
            LoadingView load = mLoadingView.findViewById(R.id.loading_img);
            load.stopLooper();
        }
    }

    private View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        View loadingView = inflater.inflate(R.layout.base_loading_layout, container, false);
        return loadingView;
    }

    protected void initView(View view) {}

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind.unbind();
        }
    }

    protected void initPresenter(){

    }

    protected void loadData(){
        //加载数据
    };

    private View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        int res = getRes();
        return inflater.inflate(res,container,false);
    }

    public abstract int getRes();


    @Override
    public void onDestroyView() {
        LogUtil.d(TAG,"Fragment onDestroyView");
        super.onDestroyView();
    }
    
}
